
const { Controller, Subscription } = require('egg');


module.exports = class UserController extends Controller {

    //* 创建一个用户
    async create() {
        const ctx = this.ctx;
        const { name, age } = ctx.request.body;
        try {
            const user = await ctx.model.User.create({ name, age });
            ctx.status = 201;
            ctx.body = user;
        } catch (error) {
            ctx.body = {
                code: 500,
                msg: error,
                data: {},
            }
        }
    }

    //* 用户列表
    async index() {
        const ctx = this.ctx;
        let data = {
            code: 0,
            msg: '',
            data: [],
        }
        try {
            const users = await ctx.model.User.findAll()
            data.code = 200
            data.data = users
        } catch (error) {
            data.code = 500
        }
        ctx.body = data
    }

}

